package com.zzj.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.base.Result;
import com.zzj.entity.Product;
import com.zzj.listener.ProductListener;
import com.zzj.model.dto.ProductAddDTO;
import com.zzj.model.dto.ProductListDTO;
import com.zzj.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2022-12-07
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private IProductService productService;
    @Resource
    private ProductListener productListener;

    @PostMapping("/list")
    public Result list(@RequestBody @Validated ProductListDTO in) {
        IPage<Product> productIPage = productService.productList(new Page<>(in.getPageNo(), in.getPageSize()), in);
        return Result.success(productIPage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody @Validated ProductAddDTO in) {
        Product product = new Product();

        boolean exists = productService.lambdaQuery().eq(Product::getBarcode, in.getBarcode()).exists();
        if (exists) {
            return Result.error(String.format("添加失败，该条码%s已经存在", in.getBarcode()));
        }
        ;
        product.setBarcode(in.getBarcode());
        product.setName(in.getName());
        product.setPrice(in.getPrice());
        product.setWholesalePrice(in.getWholesalePrice());
        product.setStock(in.getStock());
        product.setLowStock(in.getLowStock());
        product.setSupplierId(in.getSupplierId());
        product.setSupplierName(in.getSupplierName());
        product.setCategoryId(in.getCategoryId());
        product.setCategoryName(in.getCategoryName());
        product.setDescription(in.getDescription());
        product.setPictures(in.getPictures());
        boolean save = productService.save(product);
        return Result.success("添加成功");
    }

    @PostMapping("update")
    public Result update(@RequestBody @Validated Product in) {

        boolean update = productService.updateById(in);
        return Result.success("修改成功");
    }

    @PostMapping("queryByBarcode")
    public Result queryByBarcode(@RequestParam("searchKey") String searchKey) {

        Product one = productService.lambdaQuery().eq(Product::getName, searchKey).or().eq(Product::getBarcode, searchKey).one();
        return Result.success(one);
    }

    @PostMapping("deleteById")
    public Result deleteById(@RequestParam("id") String id) {
        boolean b = productService.removeById(id);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Operation(summary = "批量添加", description = "批量添加")
    @PostMapping("addByExcel")
    public Result addByExcel(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.error("上传失败，请重新选择文件");
        }
        //获取输入流
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream, Product.class, productListener).sheet().doRead();
        return Result.success();
    }
}
